package arithmetic.array;

import java.util.ArrayList;
import java.util.List;

/**
 * 求两个有序数组的交集
 */
public class GetIntersection {
    public static Integer[] getIntersection(int[] a, int[] b) {

        int i = 0, j = 0;
        List<Integer> list = new ArrayList<>();
        while (i < a.length && j < b.length) {
            if (a[i] == b[j]) {

                list.add(a[i]);
                i++;
                j++;
            } else if (a[i] > b[j]) {
                j++;
            } else {
                i++;
            }
        }
        return (Integer[]) list.toArray();
    }

    public static void main(String[] args) {
        int[] a = {2, 3, 5, 76, 23, 12, 34};
        int[] b = {2, 3, 23, 45};
        Integer[] result = getIntersection(a, b);
        System.out.println(result);
    }
}
